// 3. 写一个分开的 仓库模块
import { createSlice } from "@reduxjs/toolkit"

// 3.1 该仓库数据的约束
// interface CounterState {
//     value: number;
//     age: number;
// }
// 3.2 定义仓库的初始值
const initialState: CounterState = {
    value: 0,
    age: 20
}
// 3.3 创建仓库的分片
export const counterSlice = createSlice({
    // 命名空间
    name: 'counter',
    // 当前仓库的state
    initialState,
    // 相当于 mutacionts 操作数据的方法
    reducers: {
        increment: (state) => {
            state.value += 1
        },
        decrement: (state) => {
            state.value -= 1
        },
        add(state, action) {
            state.value += action.payload as number;
        }
    },
})

// 3.4 导出必要的数据
export const { increment, decrement, add } = counterSlice.actions
// export const selectCount = (state: any) => state.counter.value
export default counterSlice.reducer